var express = require('express');
var router = express.Router();

//引入上传文件的方法
const {
  uploadFile
} = require("../src/utils/upload");

// const adminsService = require("../src/service/admins/adminsService.js");
const adminsService = require("../src/services/admins/adminsService.js");

//引入jwt的工具类
const {
  createJwt
} = require("../src/utils/jwtUtils.js")

/* 登录 */
router.post('/login', async function (req, res) {
  let {
    username,
    password
  } = req.body;
  console.log(username, password);
  let data = await adminsService.login(username, password)
  console.log("luyou", data);
  if (data) {
    // 登录成功 就建立一个 jwt
    let jwt = createJwt({
      "username": username,
      "_id":data._id,
      "loginTime": new Date()
    });
    console.log("jwt",jwt);
    res.json({
      "code": 1,
      "msg": "登录成功",
      "data":data,
      "_t": `Bearer ${jwt}`
    })
  } else {
    res.json({
      "code": 0,
      "msg": "用户名或者密码错误"
    })
  }
});

//注册
router.post('/reg', async function (req, res) {
  let {
    username,
    password
  } = req.body;
  let data = await adminsService.reg(username, password, photo = null)
  console.log('1111', data);
  if (data.code == 1) {
    res.json({
      "code": 1,
      "msg": data.msg
    })
  } else {
    res.json({
      "code": 0,
      "msg": data.msg
    })
  }
});
//显示所有数据
router.get('/showsAll', async function (req, res) {
  // let info=  req.user
  // console.log("****===",info);
  let data = await adminsService.getallInfo()
  res.json({
    "status": 0,
    "message": "",
    "total": 180,
    "datas": data
  })
});

//详情
router.get('/xiangqing/:id', async function (req, res) {
  let {
    id
  } = req.params;
  let data = await adminsService.chazhao(id)
  console.log('1111', data);
  if (data) {
    res.json({
      "code": 1,
      "msg": data
    })
  } else {
    res.json({
      "code": 0,
      "msg": "删除失败"
    })
  }
});

//删除
router.delete('/delete/:id', async function (req, res) {
  let {
    id
  } = req.params;
  let data = await adminsService.deleteAdminsInfo(id)
  console.log('1111', data);
  if (data.deletedCount == 1) {
    res.json({
      "code": 1,
      "msg": "删除成功"
    })
  } else {
    res.json({
      "code": 0,
      "msg": "删除失败"
    })
  }
});

//修改
router.post('/update', async function (req, res) {
  let result = await uploadFile(req, {
    fileType: "images"
  });
  console.log("result", result);
  //获取上传文件的名字
  let photo = result.data.url.split("/")[2];
  console.log("photo", photo);
  // 将数据结构出来
  let {
    _id,
    adminName,
    adminPassword
  } = result.data;
  console.log(_id,
    adminName,
    adminPassword);
  // 将数据发送过去 进行修改
  let data = await adminsService.updateAdminsInfo({
    _id,
    adminName,
    adminPassword,
    photo
  });
  console.log('1111', data);
  if (data) {
    // 查找到对应的数据
    // console.log(_id);
    let resp = await adminsService.chazhao(_id)
    // console.log(resp);
    res.json({
      "code": 0,
      "msg": resp
    })
  } else {
    res.json({
      "code": 1,
      "msg": "修改失败"
    })
  };

});
// 分页
router.get('/pages', async function (req, resp) {
  let {
    currentpage,
    pagesize
  } = req.query;

  console.log(currentpage, pagesize);

  let pageBean = await adminsService.getAccessToPag({
    "currentpage": parseInt(currentpage),
    "pagesize": parseInt(pagesize)
  })

  resp.json({
    "status": 0,
    "message": "success",
    ...pageBean,
  })
});

module.exports = router;